
$(function () {

	submit_query();
	$("#query_go").click(function () {
		submit_query();
	});
});


//通过取得id="pageHelper.currentPage"这个input得到目标的page,从而提交 
function submit_query() {
	//防止页数过大或为负数
	if(!verifytargetPage()){
		return;
	}
	var url_queryGo = $("#itemsForm").attr("action");
	var queryString = $("#itemsForm").formSerialize(); //从form中得到其参数，序列化为[]这样的形式
	alert(queryString);
	htmlStr = "";
	//alert($("#listTr").text());
	$("#listTr").children().each(function (i) {
		htmlStr += $(this).attr("icode");
		htmlStr += ",";
	});
	var pramArray = htmlStr.split(","); //column的code,这里是类似['userID','userName']
	//alert(pramArray[0]);

	fillTable(url_queryGo, queryString, pramArray);
}


/**
*
*防止页数过大或为负数
*
*/
var verifytargetPage = function(){

	var targetPage = $("#targetPage").attr('value');
	var maxPage = $("#totalPage").text();
	var targetPageNum = Number($.trim(targetPage));
	var maxPageNum = Number($.trim(maxPage));
	
	if(targetPageNum>maxPageNum||targetPageNum<=0){
		alert("页数错误");
		return false;
	}
	return true;
}


/**
 *	填充
 * @param {Object} url_queryGo
 * @param {Object} queryString
 * @param {Object} pramArray
 */
function fillTable(url_queryGo, queryString, pramArray) {
	$.post(url_queryGo, queryString, function (data) {
		$("#currentPage").text(data.pageHelper.currentPage);
		$("#totalPage").text(data.pageHelper.totalPage);
		$("#itemNum").text(data.pageHelper.itemNum);
		$("#total").text(data.pageHelper.total);
		$("#itemList").empty();
		var ulist = data.pageHelper.list;
		$.each(ulist, function (key, value) {
			var trString = "<tr>";
			for (var i = 0; i < pramArray.length - 1; i++) {
				trString += "<td>";
			 // alert("value." + pramArray[i]);
				trString += eval("value." + pramArray[i]);
				trString += "</td>";
			}
			trString += "</tr>";
			$("#itemList").append(trString);
		});
	});
}

